package api

import (
	"fmt"
	"time"

	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
	"weiboxs.com/new_h_glasses/utils"
)

type ApiRetailController struct {
	ApiBaseController
}

func (c *ApiRetailController) ApiDataGrid() {
	//c.checkAuthor("retail/index")
	var params models.RetailsQueryParam

	limit,_ := c.GetInt("limit", 20)
	offset,_ := c.GetInt64("offset", 0)


	params.Limit = limit
	params.Offset = offset


	data, total := models.RetailsPageList(&params)
	c.jsonResultData(enums.JRCodeSucc, total, data)
}

//前台销售
func (c *ApiRetailController) ApiAddSales() {

	c.checkAuthor("retail/add")

	if c.Ctx.Input.IsPost() { //保存零售信息

	}
	c.Data["activeSidebarUrl"] = "/retail/add"

	f := models.OptionsQueryParam{}
	f.Status = "1"

	//获取营业员
	f.Typeid = "20"
	c.Data["business"] = models.OptionsList(&f)

	c.Data["nowDate"] = time.Now().Format("2006-01-02")

	//获取验光数据
	optid, _ := c.GetInt64("optid")
	c.Data["optid"] = optid
	//获取验光档案
	arid, _ := c.GetInt64("arid")
	c.Data["arid"] = arid

	//类型
	tid := c.GetString("tid")
	var TypeId int = 0
	if tid == "" {
		tid = "X"
		TypeId = 1
	} else {
		tid = "T"
		TypeId = 2
	}
	c.Data["tid"] = TypeId

	//生成订单数据
	stat := utils.GetDateParse(time.Now().Format("2006-01-02"))
	end := stat + 86400
	documentNumber := fmt.Sprintf("%s%s%04d", tid, time.Now().Format("06010215"), models.QueryRetailsTimeSoltSeterCount(stat, end)+1)
	c.Data["ManualNumber"] = documentNumber

}

//销售退货单
func (c *ApiRetailController) ApiRefund() {

	c.checkAuthor("retail/add")

	if c.Ctx.Input.IsPost() { //保存零售信息
		//c.saveSales()
	}
	c.Data["activeSidebarUrl"] = "/retail/add"

	f := models.OptionsQueryParam{}
	f.Status = "1"

	//获取营业员
	f.Typeid = "20"
	c.Data["business"] = models.OptionsList(&f)

	//生成订单数据
	stat := utils.GetDateParse(time.Now().Format("2006-01-02"))
	end := stat + 86400
	documentNumber := fmt.Sprintf("T%s%04d", time.Now().Format("06010215"), models.QueryRetailsTimeSoltSeterCount(stat, end)+1)
	c.Data["ManualNumber"] = documentNumber
	c.Data["nowDate"] = time.Now().Format("2006-01-02")

}

//查看消费
func (c *ApiRetailController) ApiView() {
	retails_id, _ := c.GetInt64("retails_id")
	if r, err := models.QueryRetailsDetails(retails_id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "ok", r)
	}
}

//删除消费信息
func(c *ApiRetailController) ApiDelete() {
	c.checkAuthor("retail/add")

	retails_id,_ := c.GetInt64("retails_id")

	if err := services.ConfirmDeleteRetails(retails_id, &c.curUser); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "删除成功", "")
}


func (c *ApiRetailController) ApiSendSms() {
	mobile := c.GetString("mobile")
	message := c.GetString("message")
	if  len(mobile) <= 0 || len(message) <= 0 {
		c.jsonResult(enums.JRCodeFailed, "请填写手机号及短信内容", "")
		return
	}

	argv := services.SmsArgv{}
	argv.Typeid = 2
	argv.DepartmentId = c.curUser.DepartmentId
	argv.CreatedBy = c.curUser.RealName
	argv.Mobile = mobile
	argv.Content = message

	sms, _ := services.NewSms()
	if err := sms.Send(&argv); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}

	c.jsonResult(enums.JRCodeSucc, "发送成功", "")
}